package com.hs.hlw.credit.system.business.service.impl;

import com.hs.hlw.credit.system.business.dto.ResponseDto;
import com.hs.hlw.credit.system.business.service.SysConfigService;
import com.hs.hlw.credit.system.common.enumeration.CommonStatus;
import com.hs.hlw.credit.system.data.mybatis.Pageable;
import com.hs.hlw.credit.system.data.primary.mapper.SysConfigMapper;
import com.hs.hlw.credit.system.data.primary.model.SysConfig;
import com.hualongdata.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * SysConfigService
 * @author zhb
 * @date 2018/10/29
 */
@Service
public class SysConfigServiceImpl implements SysConfigService {
    private @Autowired SysConfigMapper sysConfigMapper;

    /**
     * 分页查询
     * @param pageable
     * @return
     */
    @Override
    public ResponseDto page(Pageable pageable) {
        List<SysConfig> list = sysConfigMapper.page(pageable);
        return new ResponseDto("查询成功", pageable, list);
    }

    /**
     * 新增
     * @param sysConfig
     * @return
     */
    @Override
    public ResponseDto add(SysConfig sysConfig) {
        sysConfig.setCreateTime(new Date());
        sysConfig.setStatus(CommonStatus.STATUS_1.code);
        sysConfigMapper.insertSelective(sysConfig);
        return new ResponseDto("新增成功");
    }

    /**
     * 修改
     * @param sysConfig
     * @return
     */
    @Override
    public ResponseDto update(SysConfig sysConfig) {
        sysConfig.setUpdateTime(new Date());
        sysConfigMapper.updateByPrimaryKeySelective(sysConfig);
        return new ResponseDto("修改成功");
    }

    /**
     * 删除
     * @param list
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseDto delete(List<SysConfig> list) {
        if (list != null && !list.isEmpty()) {
            for (SysConfig sysConfig : list) {
                if (sysConfig.getId() == null) {
                    throw new BusinessException("存在未指定配置参数");
                }
            }
            for (SysConfig sysConfig : list) {
                sysConfigMapper.deleteByPrimaryKey(sysConfig.getId());
            }
        }
        return new ResponseDto("删除成功");
    }

    /**
     * 更新状态
     * @param list
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseDto updateStatus(List<SysConfig> list) {
        if (list != null && !list.isEmpty()) {
            for (SysConfig sysConfig : list) {
                if (sysConfig.getId() == null) {
                    throw new BusinessException("存在未指定配置参数");
                }
                if (!CommonStatus.contains(sysConfig.getStatus())) {
                    throw new BusinessException("存在未知的参数状态");
                }
            }
            for (SysConfig sysConfig : list) {
                SysConfig dto = new SysConfig();
                dto.setId(sysConfig.getId());
                dto.setStatus(sysConfig.getStatus());
                dto.setUpdateTime(new Date());
                sysConfigMapper.updateByPrimaryKeySelective(dto);
            }
        }
        return new ResponseDto("操作成功");
    }

    /**
     * 详情
     * @param id
     * @return
     */
    public ResponseDto detail(Integer id) {
        SysConfig sysConfig = sysConfigMapper.selectByPrimaryKey(id);
        if (sysConfig == null) {
            throw new BusinessException("记录不存在");
        }
        return new ResponseDto("查询成功", sysConfig);
    }
}